package com.groupies;

import java.io.*;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author GroupiesM
 * @date 2021/06/09
 * @introduction
 */
public class Main {
    //获取相对路径 => ${PATH}
    private static String root_filePath = System.getProperty("user.dir");
    //测试环境根目录 => ${PATH}/ktr文件
    private static String test_FilePath = root_filePath + File.separator + "ktr文件" + File.separator + "ktr测试环境";
    //测试环境表目录 => ${PATH}/ktr文件/ktr生产环境
    private static String test_FileFullPath = "";
    //生产环境表目录 => ${PATH}/ktr文件/ktr测试环境/组织机构_地方
    private static String production_FileFullPath = "";
    //生产环境根目录 => ${PATH}/ktr文件/ktr测试环境
    private static String production_FilePath = root_filePath + File.separator + "ktr文件" + File.separator + "ktr生产环境";
    //生产环境表目录 => ${PATH}/ktr文件/ktr测试环境/组织机构_地方/r_db_3031271115.ktr
    private static String tableName = "";
    //存放ktr的目录名称 => 组织机构_地方
    private static String workPath = "";
    //测试环境File对象 => test_FilePath = new File(test_FilePath);
    private static File test_File;
    //生产环境File对象 => production_FilePath = new File(production_FilePath);
    private static File production_File;
    //测试环境File-ktr文件对象 => new File(test_FileFullPath, tableName + ".ktr");
    private static File test_KtrFile;
    //生产环境File-ktr文件对象 => production_ktrFile = new File(production_FileFullPath, tableName + ".ktr");
    private static File production_ktrFile;
    //ktr全称 => r_db_3031271112.ktr
    private static String ktrName = "";

    public static void main(String[] args) throws IOException {
        //printEnv();

        Scanner sc = new Scanner(System.in);

        //测试环境File对象
        test_File = new File(test_FilePath);
        //生产环境File对象
        production_File = new File(production_FilePath);

        File ktrFilePath = new File(root_filePath + File.separator + "ktr文件");
        deleteFile(ktrFilePath);
        boolean mkdir1 = test_File.mkdirs();
        boolean mkdir2 = production_File.mkdirs();

        System.out.println("==============================LogStart============================================");
        System.out.println(test_FilePath +
                                   "\t=>\t创建测试目录:" + mkdir1 +
                                   "\n" + production_FilePath +
                                   "\t=>\t创建生产目录:" + mkdir2
        );

        System.out.println("***************************请输入表信息,示例如下:****************************************" +
                                   "\nr_db_3031271112,组织机构_央企" +
                                   "|r_db_3031271115,组织机构_地方|r_db_3031271116,组织机构_地方" +
                                   "\n*****************************************************************************************");
        System.out.println("==============================LogEnd==============================================");
        String next = sc.next();
        System.out.println("==============================LogStart============================================");
        /** 按照分隔符分割每个表的数据，数据分割后如下，再按照逗号分隔，
         *  第一个值作为表名，第二个值作为文件工作目录
         * r_db_3031271112,组织机构_央企
         * r_db_3031271115,组织机构_地方
         * r_db_3031271116,组织机构_地方 */
        String[] columnInfo = next.split("\\|");
        //测试环境创建ktr文件及目录
        testCreateKtr(columnInfo);
        //生产环境创建ktr文件及目录
        productCreateKtr(columnInfo);
        System.out.println("创建完成，退出程序");
        System.out.println("==============================LogEnd==============================================");
    }

    /**
     * @introduction 测试环境创建ktr文件及目录
     * @param columnInfo
     * @throws IOException
     */
    private static void testCreateKtr(String[] columnInfo) throws IOException {
        for (String ci : columnInfo) {
            String[] columnDetail = ci.split(",");
            for (int i = 0; i < columnDetail.length; i++) {
                if (i == 0) {//表名
                    tableName = columnDetail[i];
                } else if (i == 1) {//路径
                    workPath = columnDetail[i];
                }
            }
            //ktr文件名 => r_db_3031271112.ktr
            ktrName = tableName + ".ktr";
            //【测试环境】ktr文件路径 => ${PATH}\ktr文件\ktr测试环境\组织机构_央企
            test_FileFullPath = test_FilePath + File.separator + workPath;
            //【测试环境】File对象
            test_File = new File(test_FileFullPath);
            //【测试环境】创建目录 => ${PATH}\ktr文件\ktr测试环境\组织机构_央企
            test_File.mkdirs();
            //【测试环境】创建文件对象
            test_KtrFile = new File(test_FileFullPath, ktrName);
            //打印日志
            System.out.println("正在创建..." + test_FileFullPath + File.separator + ktrName);
            //【测试环境】删除文件 => ${PATH}\ktr文件\ktr测试环境\组织机构_央企\r_db_3031271112.ktr
            test_KtrFile.delete();
            //【测试环境】创建文件 => ${PATH}\ktr文件\ktr测试环境\组织机构_央企\r_db_3031271112.ktr
            test_KtrFile.createNewFile();

            //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(test_FileFullPath + File.separator + ktrName));
            //byte[] bytes = KtrSource.ktrSourceTest.replaceAll("\\$\\{TABLE_NAME\\}",tableName).getBytes();
            //bos.write(bytes);//写入字符数组
            //bos.close();

            //指定字符缓冲输出流 （kettle仅支持打开utf-8格式ktr文件，windows默认为GBK所以一定要输出UTF-8格式）
            OutputStreamWriter oswUTF8 = new OutputStreamWriter(new FileOutputStream(test_FileFullPath + File.separator + ktrName), "utf-8");
            oswUTF8.write(KtrSource.ktrSourceTest.replaceAll("\\$\\{TABLE_NAME\\}", tableName));
            oswUTF8.close();
        }
    }

    /**
     * @introduction 生产环境创建ktr文件及目录
     * @param columnInfo
     * @throws IOException
     */
    private static void productCreateKtr(String[] columnInfo) throws IOException {
        for (String ci : columnInfo) {
            String[] columnDetail = ci.split(",");
            for (int i = 0; i < columnDetail.length; i++) {
                if (i == 0) {//表名
                    tableName = columnDetail[i];
                } else if (i == 1) {//路径
                    workPath = columnDetail[i];
                }
            }
            //ktr文件名 => r_db_3031271112.ktr
            ktrName = tableName + ".ktr";
            //【生产环境】ktr文件路径 => ${PATH}\ktr文件\ktr生产环境\组织机构_央企
            production_FileFullPath = production_FilePath + File.separator + workPath;
            //【生产环境】File对象
            production_File = new File(production_FileFullPath);
            //【生产环境】创建目录 => ${PATH}\ktr文件\ktr生产环境\组织机构_央企
            production_File.mkdirs();
            //【生产环境】ktr文件路径 => ${PATH}\ktr文件\ktr生产环境\组织机构_央企
            production_FileFullPath = production_FilePath + File.separator + workPath;
            //【生产环境】创建文件对象
            production_ktrFile = new File(production_FileFullPath, ktrName);
            //打印日志
            System.out.println("正在创建..." + production_FileFullPath + File.separator + ktrName);
            //【生产环境】删除文件 => ${PATH}\ktr文件\ktr生产环境\组织机构_央企\r_db_3031271112.ktr
            production_ktrFile.delete();
            //【生产环境】创建文件 => ${PATH}\ktr文件\ktr生产环境\组织机构_央企\r_db_3031271112.ktr
            production_ktrFile.createNewFile();

            //指定字符缓冲输出流 （kettle仅支持打开utf-8格式ktr文件，windows默认为GBK所以一定要输出UTF-8格式）
            OutputStreamWriter oswUTF8 = new OutputStreamWriter(new FileOutputStream(production_FileFullPath + File.separator + ktrName), "utf-8");
            oswUTF8.write(KtrSource.ktrSourceProduction.replaceAll("\\$\\{TABLE_NAME\\}", tableName));
            oswUTF8.close();
        }
    }

    /**
     * @introduction 遍历File目录，删除所有文件
     * @param f
     */
    public static void deleteFile(File f) {//传一个File对象f进去 因为要调用其方法
        if (f != null) {//判断f数据是否为空 因为可能有人会传null值进来
            if (f.exists()) {//判断该抽象路径的目录和文件是否存在
                if (f.isDirectory()) {//判断是否是目录
                    File[] listFiles = f.listFiles();//调用listFiles()方法返回一个File对象组成的数组集合
                    if (listFiles == null) {//如果该数组为Null说明没有文件和目录对象 就是一个空的文件夹
                        f.delete();//直接删除
                    } else {//不为空的文件夹
                        for (File file : listFiles) {//遍历该集合
                            deleteFile(file);//遍历到的每个File对象传入demo()方法
                        }
                        f.delete();//最后将这个不为空的文件夹删除
                    }
                } else {//不是目录可能是是文件或者不存在
                    f.delete();//直接删除
                }
            } else {//为Null
                return;//结束方法调用
            }
        }
    }

    /**
     * @introduction 打印环境变量，测试编码等信息
     */
    private static void printEnv() {
        //获取环境变量
        Map<String, String> envs = System.getenv();
        envs.forEach((key, value) -> {
            //System.out.println("env【key是: " + key + " ; " + "value是: " + value + "】");
        });

        //获取jvm属性
        Properties properties = System.getProperties();
        properties.forEach((key, value) -> {
            System.out.println("jvm【key是: " + key + " ; " + "value是: " + value + "】");
        });
    }
}
